; 3DTicTacToe installer script for NSIS >=2.45
; 
; Required plugins:
;   processwork.dll http://www.esanu.name/software/index.php/nsis-kill-process/
;   version.dll     http://nsis.sourceforge.net/Version_plug-in

!include "MUI.nsh"
!include "LogicLib.nsh"

!define PRODUCT_NAME "3DTicTacToe"
!define PRODUCT_VERSION "dev"
!define PRODUCT_PUBLISHER "Raffael Thome"
!define PRODUCT_WEB_SITE "http://code.google.com/p/3dtictactoe/"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\3DTicTacToe.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor /SOLID lzma
SetCompressorDictSize 32
SetCompress auto
SetDatablockOptimize on

CRCCheck force

BrandingText "3DTicTacToe ${PRODUCT_VERSION} - Copyright (c) ${PRODUCT_PUBLISHER}"

!define MUI_ABORTWARNING

!define MUI_ICON "3dtictactoe.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

!define MUI_WELCOMEFINISHPAGE_BITMAP "welcomepage.bmp"

!insertmacro MUI_PAGE_WELCOME
!define MUI_LICENSEPAGE_CHECKBOX
!insertmacro MUI_PAGE_LICENSE "COPYING.txt"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!define MUI_FINISHPAGE_RUN "$INSTDIR\3DTicTacToe.exe"
!define MUI_FINISHPAGE_RUN_TEXT "Run 3DTicTacToe"
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "English"

!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "3DTicTacToe-${PRODUCT_VERSION}-Installer.exe"
InstallDir "$PROGRAMFILES\${PRODUCT_NAME}"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUninstDetails show

!macro CheckAppRunning APP_PROCESS_NAME
  car_Check:
    Push "${APP_PROCESS_NAME}"
    processwork::ExistsProcess
    Pop $0
    IntCmp $0 0 car_Done
	
	MessageBox MB_ICONEXCLAMATION|MB_RETRYCANCEL "3DTicTacToe is currently running. You have to close it in order to proceed." IDRETRY car_Check IDCANCEL car_Kill
    
  car_Kill:
	Abort
  
  car_Done:
!macroend

  var MajorVersion
  var MinorVersion
  var BuildNumber
  var PlatformID
  var CSDVersion

!macro CheckOpSys
  
  Version::GetWindowsVersion
  Pop $MajorVersion
  Pop $MinorVersion
  Pop $BuildNumber
  Pop $PlatformID
  Pop $CSDVersion
  
  MessageBox MB_OK "$PlatformID-platform, version $MajorVersion.$MinorVersion, build $BuildNumber, $CSDVersion"

!macroend

!macro CheckNetFx
  ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"
  StrCmp "$0" "1" cnf_Installed
  DetailPrint "Installing .Net Framework 4, this will take a few minutes."
  DetailPrint "Please stand by ..."
  GetTempFileName $0
  NSISdl::download "http://download.microsoft.com/download/1/B/E/1BE39E79-7E39-46A3-96FF-047F95396215/dotNetFx40_Full_setup.exe" $0
  
  ExecWait "$0 /passive /norestart"
  Delete $0
  Goto cnf_Done

  cnf_Installed:
    DetailPrint ".Net Framework 4 found, continuing ..."

  cnf_Done:
!macroend

Section "3DTicTacToe" SEC01
  
  !insertmacro CheckNetFx

  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  
  File "..\bin\3DTicTacToe.exe"
  File "..\bin\3DTicTacToe.Core.dll"
  
  File "..\bin\Microsoft.WindowsAPICodePack.dll"
  File "..\bin\Microsoft.WindowsAPICodePack.Shell.dll"
  File "..\bin\Lidgren.Network.dll"
  File "..\bin\Newtonsoft.Json.dll"
  
  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\3DTicTacToe.lnk" "$INSTDIR\3DTicTacToe.exe"
SectionEnd

Section -AdditionalIcons
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\3DTicTacToe.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\3DTicTacToe.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

Function .onInit
  !insertmacro CheckOpSys
  !insertmacro CheckAppRunning "3DTicTacToe.exe"
FunctionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "3DTicTacToe has been successfully removed."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Do you want to uninstall 3DTicTacToe?" IDYES +2
  Abort
  
  !insertmacro CheckAppRunning "3DTicTacToe.exe"
FunctionEnd

Section Uninstall
  Delete "$INSTDIR\*" ; kill all files!

  Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  Delete "$SMPROGRAMS\${PRODUCT_NAME}\3DTicTacToe.lnk"

  RMDir "$SMPROGRAMS\3DTicTacToe"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  
  SetAutoClose true
SectionEnd